/**
 * Created by IntelliJ IDEA.
 * User: user
 * Date: 10.10.12
 * Time: 10:59
 * To change this template use File | Settings | File Templates.
 */
package zakladki.kontynenty {
import AS3.helpers.FlagImageHelper;

import flash.display.Bitmap;

import flash.display.BitmapData;

import mx.containers.Canvas;
import mx.controls.Alert;
import mx.controls.Image;
import mx.core.FlexGlobals;

import org.osmf.layout.AbsoluteLayoutMetadata;

import zakladki.ChooseCountryPanel;

public class ContinentPanel extends Canvas{

    protected var mainApp:Fossilminator = FlexGlobals.topLevelApplication as Fossilminator;

    protected var bm:Bitmap;
    protected var bmd:BitmapData;
    protected var currentCountryColor:String;

    public function ContinentPanel() {

    }

    public function setCurrentCountryColor(color:String):void {
        currentCountryColor = color;
    }

    public function setFlagsPosition(countryNamePl:String, countryName:String):void {
        var objParam:Object = {nazwa_panstwa:countryName ,countryColor:""};
        FlagImageHelper.fillFlagImageAndNameByNativeName(objParam);
        onCountrySelectedOnList(objParam.countryColor);
    }

    public function hideFlags():void {
        getUndercoatFlagImage().visible = false;
        getCountryFlagImage().visible = false;
    }

    protected function continentCreationComplete():void {
        mainApp.chooseCountryPanel.continentCreationCompleteHandler();
    }

    protected function onCountrySelectedOnList(color:String):void {
        try {
            mainApp.chooseCountryPanel.onCountrySelectedOnList(color);
        } catch(e:Error) {
            Alert.show("Error invoking chooseCountryPanel: ")
        }
    }

    protected function onMouseMove():void {
        currentCountryColor = getCurrentCountryColor();
        focusCountry(currentCountryColor);
        mainApp.chooseCountryPanel.onMouseMoved(currentCountryColor);
    }

    protected function onClick():void {
        try {
            var objVar:Object = {kod_koloru:currentCountryColor ,country:"" , nazwa:""};
            FlagImageHelper.fillFlagImageAndNameByColor(objVar, getCountryFlagImage(),false);
            if (objVar.country != "") {
                moveFlags();
                mainApp.chooseCountryPanel.countryClicked(objVar.country, currentCountryColor);
            }
        } catch(e:Error) {
            Alert.show("Error click: " + e.message);
        }
    }

    protected function onDoubleClick():void {
        var objVar:Object = {kod_koloru:currentCountryColor ,country:"" , nazwa:""};
        FlagImageHelper.fillFlagImageAndNameByColor(objVar, getCountryFlagImage(),false);
        mainApp.chooseCountryPanel.countryDoubleClicked(objVar.country);
    }

    private function getCurrentCountryColor():String {
        var countryColor:String = new String();

        var undercoat:Image = getUndercoatImage();
        var mainMap:Image = getMainMapImage();

        bm = undercoat.content as Bitmap;
        bmd = new BitmapData(undercoat.contentWidth, undercoat.contentHeight);
        bmd.draw(bm.bitmapData);

        var mouseYRatio:Number = undercoat.contentHeight / mainMap.height;

        var color:int = bmd.getPixel(mainMap.mouseX, mainMap.mouseY * mouseYRatio);
        var colorStr:String = color.toString(16).toUpperCase();
        colorStr = "000000" + colorStr;
        countryColor = "#" + colorStr.substr(colorStr.length - 6);
        return countryColor;
    }

    private function focusCountry(countryColor:String):void {
        hideCountries();
        focusSelectedCountry(countryColor);
    }

    protected function hideCountries():void {

    }

    protected function focusSelectedCountry(countryColor:String):void {

    }

    protected function moveFlags():void {

    }

    protected function getMainMapImage():Image {
        return null;
    }

    protected function getUndercoatImage():Image {
        return null;
    }

    protected function getUndercoatFlagImage():Image {
        return null;
    }

    protected function getCountryFlagImage():Image {
        return null;
    }


}
}
